#!/usr/bin/env node

const fs = require('fs')
const process = require('child_process')

const ls = process.spawn('ls')
ls.stdout.on('data', data => {
  const filesTotal = data.toString().split('\n')
  let json = '{'
  const files = []
  filesTotal.forEach(item => {
    if (item.endsWith('.json') && item !== 'theme.json') {
      files.push(item)
    }
  })
  files.forEach((item, index) => {
    json += '"' + item.split('.')[0] + '": '
    json += fs.readFileSync(item)
    json = json.replace(/\s*/g,'')
    if (index < files.length - 1) {
      json += ','
    }
  })
  json += '}'
  fs.writeFileSync('theme.json', json)
})

ls.stderr.on('data', data => {
  console.error(data.toString())
})

ls.on('close', code => {
  console.log('process closed with ' + code)
})